<template>
  <div>
    <Home-Header :city="city"></Home-Header>
    <Home-Swiper :swiperList="swiperList"></Home-Swiper>
    <Home-Icons :iconsList="iconsList"></Home-Icons>
    <Home-Recommend :recommendList="recommendList"></Home-Recommend>
    <Home-Weekend :weekendList="weekendList"></Home-Weekend>
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import { mapState } from 'vuex' // mapState映射state状态中的值可以判断是否发送ajax

export default {
  name: 'Home',
  components: {
    HomeHeader: HomeHeader,
    HomeSwiper: HomeSwiper,
    HomeIcons: HomeIcons,
    HomeRecommend: HomeRecommend,
    HomeWeekend: HomeWeekend
  },
  computed: {
    ...mapState({
      curCity: 'city'
    })
  },
  data: function () {
    return {
      city: '',
      swiperList: [],
      iconsList: [],
      recommendList: [],
      weekendList: [],
      lastCity: ''
    }
  },
  methods: {
    getHomeInfo: function () {
      axios.get('http://localhost:8080/static/mock/index.json?city=' + this.curCity)
        .then(this.getHomeInfoSuccess)
    },
    getHomeInfoSuccess: function (res) {
      if (res.data.ret && res.data.data) {
        let data = res.data.data
        this.city = data.city
        this.swiperList = data.swiperList
        this.iconsList = data.iconsList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted: function () {
    this.lastCity = this.curCity
    this.getHomeInfo()
  },
  activated: function () {
    if (this.lastCity !== this.curCity) {
      this.lastCity = this.curCity
      this.getHomeInfo()
    }
  }
}
</script>
